/**
 * 用户列表 add by gsh
 */
$(function() {
	// toolbar添加
	$("#btnAdd").click(function() {
		var index = msgWnd.open({
			title : "添加用户信息",
			skin : 'layui-layer-lan',
			type : 2,
			shade : 0.5,
			area : [ "530px", "550px" ],
			maxmin : true,
			content : '/user/add',
			zIndex : layer.zIndex
		});
	});

	// toolbar修改
	$("#btnEdit").click(function() {
		var checked = getChecked();
		if (checked.length != 1) {
			msgTools.alert("错误", "请选择1条要操作的记录！", 500);
			return;
		}
		accountEdit(checked[0]);
	});

	// 全选
	$("#chkAll").change(function() {
		var chkStatus = $(this).is(":checked");
		$("[id^=chk]:not(#chkAll)").each(function() {
			this.checked = chkStatus;
		});
	});

	// toolbar批量禁用
	$("#btnDisable").click(function() {
		var checked = getChecked();
		if (checked.length == 0) {
			msgTools.alert("错误", "请选择要操作的记录！", 500);
			return;
		}
		$.ajax({
			type : "POST",
			url : "/user/disable/batch",
			data : JSON.stringify(checked),
			contentType : "application/json",
			dataType : "json",
			success : function(msg) {
				if (msg.data.status == 1) {
					msgTools.alert("批量禁用", "禁用成功。", 1000);
					if(typeof(reloadPg)!="undefined")
						reloadPg();
					else
						window.location.href = window.location.href;
					
				} else {
					msgTools.alert("批量禁用", "禁用失败。", 2000);
				}
			}
		});
	});

	// toolbar批量启用
	$("#btnEnable").click(function() {
		var checked = getChecked();
		if (checked.length == 0) {
			msgTools.alert("错误", "请选择要操作的记录", 500);
			return;
		}
		$.ajax({
			type : "POST",
			url : "/user/enable/batch",
			data : JSON.stringify(checked),
			contentType : "application/json",
			dataType : "json",
			success : function(msg) {
				if (msg.data.status == 1) {
					msgTools.alert("批量启用", "启用成功。", 1000);
					if(typeof(reloadPg)!="undefined")
						reloadPg();
					else
						window.location.href = window.location.href;
				} else {
					msgTools.alert("批量启用", "启用失败。", 2000);
				}
			}
		});
	});
});

function getChecked() {
	var map = $("[id^=chk]:not(#chkAll)").map(function() {
		if (this.checked)
			return this.value;
	});
	return map.get();
}

function viewUser(id) {
	var index = msgWnd.open({
		title : "查看用户信息",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "510px", "550px" ],
		maxmin : true,
		content : '/user/view/' + id,
		zIndex : layer.zIndex
	});	
}

function accountEdit(id) {
	var index = msgWnd.open({
		title : "帐户管理",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "510px", "420px" ],
		maxmin : true,
		content : '/account/' + id,
		zIndex : layer.zIndex
	});	
}

function exportDat(){
	var el=$("#qryCond").children().clone();
	var frm = $("#exportForm").html(el);
	frm.hide();
	frm.submit();
}



